package com.bdqn.carrental.controller;


import com.bdqn.carrental.pojo.UserEntity;
import com.bdqn.carrental.service.UserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.omg.CosNaming.BindingIteratorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller //证明是controller层并且返回json
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    private String meg=null;//页面弹框标识符
    /**
     * 查询用户信息并分页
     * @return
     */
    @RequestMapping(value = "/userlist",method = RequestMethod.GET)
    public String  userEntityList(Model model,
             @RequestParam(value = "userName",required = false)String _userName,
             @RequestParam(value = "pageNum",required = false)String _pageNum  ) {
        Integer pageNum=1;
        if(_pageNum!=null){
            pageNum=Integer.parseInt(_pageNum);
        }
        String userName="";
        if (_userName!=null){
            userName=_userName;
        }
        //引入分页查询，使用PageHelper分页功能
        //在查询之前传入当前页，然后多少记录
        PageHelper.startPage(pageNum,4);
        //startPage后紧跟的这个查询就是分页查询
        List<UserEntity> list =userService.findAll(userName);
        //使用PageInfo包装查询结果，只需要将pageInfo交给页面就可以7
        PageInfo<UserEntity> page=new PageInfo<UserEntity>(list);
        model.addAttribute("pageInfo",page);
        model.addAttribute("meg",meg);
        model.addAttribute("username",userName);
		meg=null;
        return "yg-jbxx";
    }

    /**
     * 修改提交，成功再跳转到信息列表
     * @param model
     * @param username
     * @return
     */
    @RequestMapping(value = "/infolist",method = RequestMethod.GET)
    public  String updlist(Model model,@RequestParam(value ="username")String username){
try {
    UserEntity userEntity=userService.Username(username);
    model.addAttribute("userEntity",userEntity);
}catch (Exception e) {
    e.printStackTrace();
}
return  "userupd";
    }


    /**
     * 新增用户信息
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/usersave")
    public  String save(@Valid UserEntity userEntity, BindingResult bindingResult, Model model) {
        try {
            if(userService.saveuser(userEntity)>0){
                meg="add";
                return "redirect:/user/userlist";
            }
            userService.saveuser(userEntity);
        } catch (Exception e) {
                e.printStackTrace();
                System.out.print(e);
        }

        return "gl-khgl-tc";
    }

    /**
     * 删除信息
     * @param username
     * @retur
     */
    @RequestMapping(value = "/userdel")
    public  String del(@RequestParam(value = "username") String username){
        if(userService.deluser(username)>0){
            meg="del";
            return "redirect:/user/userlist";
        }
        System.out.print("删除失败");
        return "yg-jbxx";
    }

    /**
     * 修改操作
     */
    @RequestMapping(value ="/userupd")
    public  String update(UserEntity userEntity,Model model){
        try {
            if(userService.upduser(userEntity)>0){
                meg="upd";
                return "redirect:/user/userlist";
            }
            model.addAttribute("list",userEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "userupd";
    }




  /*  //验证标准号是否同名
    @ResponseBody
    @RequestMapping("/check")
    public Object check(@RequestParam(value = "username") String username) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        String result = "false";
        if ((standardService.findByPage(standard, 0, 4)).size() != 0) {
            result = "false";
            return result;
        } else {
            result = "true";
            return result;
        }
    }*/

}
